<!--
 * @Author: xuna
 * @Descripttion: 生日霸屏模板
-->
<template>
  <div v-if="isCare == 2" v-loading="loading" class="dominat-container">
    <div class="dominat-set">
      <div class="dominat-img">
        <img src="@/assets/img/domain_bg.png" alt="" />

        <div v-if="videoPath" class="video-panel">
          <video
            style="width:100%;height:100%"
            :src="videoPath"
            muted
            controls
            playsinline
            x5-video-player-type="h5"
            name="media"
            data-video="0"
          ></video>
        </div>
        <div v-else class="video-panel default-panel">
          <p>未添加视频</p>
          <p>快去定制生日祝福视频吧！</p>
        </div>
      </div>

      <div class="dominat-info">
        <p class="title">生日祝福霸屏</p>
        <span class="tips">
          • 设置生日祝福霸屏后，每个员工都会在生日当天收到来自企业的定制祝福。
        </span>
        <span>• 发送时间：</span>
        <p>员工生日当天登录【薪福利】App端的时间点</p>
        <span>• 主题：</span>
        <p>电脑端推送生日贺卡，寿星登录即可查看。</p>
        <span>• 霸屏样式：</span>
        <p>如右侧示意图，您可进行自定义增加视频祝福。</p>
        <span>• 自定义祝福视频：</span>
        <p>
          建议上传16：9，分辨率1280*720及以上视频
        </p>
        <p>限制上传最长5分钟时长，不超过100M的mp4格式视频文件</p>
        <div class="handle-btn">
          <input v-show="false" id ref="uploadWidget" required type="file" name />
          <wf-el-auth-button
            v-if="!videoPath && !isShowGress"
            auth-key="WALFARE-GHNR-BPTP"
            class="settbtn save-btn"
            type="primary"
            @click="doUpload(false)"
          >
            添加视频
          </wf-el-auth-button>
          <div v-if="videoPath && !isShowGress" class="upload-btn">
            <!-- 下期需求 -->
            <!-- <el-button class="settbtn" @click="previewQr">
              预览效果
            </el-button> -->
            <wf-el-auth-button
              v-if="isSave"
              auth-key="WALFARE-GHNR-BPTP"
              class="settbtn"
              @click="dialogVisible = true"
            >
              删除视频
            </wf-el-auth-button>
            <wf-el-auth-button
              auth-key="WALFARE-GHNR-BPTP"
              class="settbtn save-btn"
              @click="doUpload(true)"
            >
              重传视频
            </wf-el-auth-button>
          </div>
          <wf-el-auth-button
            auth-key="WALFARE-GHNR-BPTP"
            class="settbtn save-btn"
            :class="{ default: isSave || !videoPath }"
            :disabled="isSave || !videoPath"
            @click="saveDominaVideo"
          >
            保存
          </wf-el-auth-button>
        </div>
        <div v-if="isShowGress" class="progress-panel">
          <el-progress :percentage="percentComplete" class="progress-line"></el-progress>
          <div class="cancle-btn" @click="cancleSource"><i class="el-icon-close"></i></div>
        </div>
      </div>
    </div>
    <!--  -->
    <!-- <div class="dominat-btn">
      <el-button
        class="settbtn save-btn"
        :class="{ default: isSave || !videoPath }"
        :disabled="isSave || !videoPath"
        @click="saveDominaVideo"
      >
        保存
      </el-button>
    </div> -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>删除后，列表不再保留此公告信息，您是否确认删除？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="手机扫一扫预览效果" :visible.sync="previewVisible" class="previewVisible">
      <div id="qrcode" ref="qrcode" class="preview-panel">
        <!-- <img :src="qrcodeUrl" alt="" /> -->
      </div>
    </el-dialog>
    <el-drawer
      :title="loadTitle"
      :visible="isVisible"
      direction="rtl"
      size="40%"
      :before-close="loadhandleClose"
      :wrapper-closable="false"
    >
      <Uploader
        ref="mychild"
        :is-visible.sync="isVisible"
        :edit-data="editData"
        @formData="formData"
        @getVideoInfo="getVideoInfo"
      />
    </el-drawer>
  </div>
</template>

<script>
  import { saveAndUpdate, queryBirthdayScreen, careModelDelete } from '@/api/careModule';
  import QRCode from 'qrcodejs2';
  import Uploader from '@/components/Uploader/Uploader';
  export default {
    components: {
      Uploader,
    },
    props: {
      isCare: {
        type: String,
        required: true,
      },
    },
    data() {
      return {
        videoPath: '',
        fileSize: '',
        percentComplete: 0, // 视频上传进度
        duration: '', // 视频时长
        isShowGress: false,
        dialogVisible: false,
        isSave: false,
        previewVisible: false,
        videoInfo: {},
        isRetran: false,
        qrcodeUrl: '',
        source: null,
        isVisible: false,
        editData: null,
        loadTitle: '上传素材',
        loading: false,
      };
    },
    watch: {
      previewVisible(val) {
        if (!val) {
          this.$refs.qrcode.innerHTML = '';
        }
      },
    },
    activated() {
      this.initDomina();
    },
    methods: {
      initDomina() {
        queryBirthdayScreen().then(res => {
          if (res.status == 200) {
            if (res.data.length) {
              this.videoInfo = res.data[0];
              this.videoPath = res.data[0].videoPath;
              this.isSave = true;
            } else {
              this.videoInfo = {};
              this.videoPath = '';
            }
          }
        });
      },
      beforeClose() {
        this.$emit('closeDrawer', false);
      },
      doUpload(flag) {
        this.isRetran = flag;
        // this.$refs.uploadWidget.click();
        // 视频组件上传
        console.log(this.videoInfo);
        if (flag) {
          this.editData = { id: this.videoInfo.videoId };
        } else {
          this.editData = null;
        }
        this.isVisible = true;
      },
      confirmDelete() {
        this.loading = true;
        try {
          careModelDelete({ id: this.videoInfo.id }).then(res => {
            this.loading = false;
            if (res.status == 200) {
              this.dialogVisible = false;
              this.initDomina();
              this.editData = null;
              this.$message({
                type: 'success',
                message: `删除成功!`,
              });
            }
          });
        } catch (error) {
          this.loading = false;
        }
      },
      qrcode() {
        let qrcodeUrl = new QRCode('qrcode', {
          width: 148, // 二维码宽度，单位像素
          height: 148, // 二维码高度，单位像素
          text: 'https://www.baidu.com/', // 生成二维码链接
        });
        console.log(qrcodeUrl);
      },
      previewQr() {
        // 预览
        this.previewVisible = true;
        this.$nextTick(() => {
          this.qrcode();
        });
      },
      cancleSource() {
        this.isShowGress = false;
        this.source.cancel('取消请求');
      },
      saveDominaVideo() {
        if (this.isSave || !this.videoPath) return;
        // 保存
        let objData = {
          modelType: 6,
          dateType: 1,
          videoPath: this.videoPath,
          videoId: this.videoInfo.videoId,
          width: this.videoInfo.width,
          height: this.videoInfo.height,
        };
        if (this.isRetran) {
          objData = Object.assign(objData, {
            id: this.videoInfo.id,
            version: this.videoInfo.version,
          });
        }
        this.loading = true;
        saveAndUpdate(objData).then(res => {
          this.loading = false;
          if (res.status == 200) {
            this.isSave = true;

            this.initDomina();
            this.$message({
              type: 'success',
              message: `保存成功!`,
            });
          }
        });
      },
      loadhandleClose() {
        this.isVisible = false;
        this.editData = null;
      },
      // 获取表单里面数据
      formData(values) {
        this.isVisible = values;
      },
      getVideoInfo(val) {
        this.videoInfo = Object.assign(this.videoInfo, {
          ...val,
        });
        console.log(val, '-------------获取视频信息');
        this.videoPath = val.videoPath;
        this.isSave = false;
      },
    },
  };
</script>

<style lang="scss" scoped>
  @import '~@/styles/mixin.scss';
  .dominat-container {
    /deep/.el-drawer {
      border-radius: 14px 0 0 14px;
    }
    /deep/.el-drawer__header {
      display: flex;
      align-items: center;
      @include font(16px, #34363b, bold);
      height: 60px;
      background: rgba(255, 255, 255, 1);
      box-shadow: 0px 1px 12px 0px rgba(194, 194, 198, 0.31);
      padding: 0 0 0 14px;
    }
    /deep/.el-drawer__body {
      padding: 16px 10px 0 10px !important;
      box-sizing: border-box;
      overflow-y: auto;
    }
    /deep/.el-textarea,
    /deep/.el-select--medium {
      width: auto !important;
    }
    /deep/.el-dialog {
      border-radius: 7px;
    }
    .previewVisible {
      /deep/.el-dialog {
        @include wh(267px, 297px);
      }
      /deep/.el-dialog__header {
        text-align: center;
        @include font(16px, #34363b, 800);
      }
      /deep/.el-dialog__body {
        display: flex;
        justify-content: center;
      }
      .preview-panel {
        @include wh(180px, 180px);
        border-radius: 7px;
        border: 1px solid #f4f0f3;
        padding: 16px;
        box-sizing: border-box;
        > img {
          @include wh(148px, 147px);
        }
      }
    }
    .dominat-set {
      display: flex;
      padding: 14px 16px 16px;
      box-sizing: border-box;
      border-radius: 7px;
      background-color: #fff;
      font-weight: 400;
      min-height: 453px;
      .dominat-img {
        position: relative;
        @include wh(194px, 410px);
        margin-right: 42px;
        .video-panel {
          @include wh(159px, 96px);
          position: absolute;
          top: 67px;
          left: 50%;
          transform: translateX(-50%);
        }
        .default-panel {
          background: #5a5a5a;
          border-radius: 6px;
          display: flex;
          align-items: center;
          justify-content: center;
          flex-direction: column;
          p {
            padding: 0;
            margin: 0;
            @include font(12px, #7f848b, 400);
            text-align: center;
            &:first-of-type {
              @include font(14px, #fff, 600);
              margin-bottom: 10px;
            }
          }
        }
      }
      .dominat-info {
        font-size: 13px;
        p {
          color: #353739;
          margin: 0;
          padding: 0;
          line-height: 20px;
          &.title {
            @include font(14px, #34363b, 800);
          }
        }
        span {
          color: #7f848b;
          line-height: 28px;
          display: block;
          margin-top: 15px;
          &.tips {
            margin-top: 4px;
          }
        }
        .handle-btn {
          display: flex;
          margin-top: 30px;
          .upload-btn {
            display: flex;
            &.save-btn {
              background-color: #3771e9;
              color: #fff;
            }
          }
        }
        .settbtn {
          @include wh(94px, 36px);
          margin: 0 17px 0 0px !important;
        }
      }
    }
    .dominat-btn {
      margin-top: 15px;
      display: flex;
      justify-content: flex-end;
    }
    .settbtn {
      @include wh(100px, 40px);
      background: rgba(255, 254, 255, 1);
      border-radius: 7px;
      border: 1px solid rgba(232, 228, 232, 1);
      @include font(14px, #353739, 400);
      &.save-btn {
        background-color: #3771e9;
        color: #fff;
        margin-left: 12px;
        &.default {
          background-color: #fff;
          color: #e8e4e8;
        }
      }
    }

    .progress-panel {
      @include wh(466px, 52px);
      border-radius: 12px;
      border: 1px solid #f0f0f0;
      padding: 0 20px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .cancle-btn {
        @include wh(26px, 26px);
        border: 2px solid #f8f7fa;
        border-radius: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
        i {
          color: #8f8f92;
          font-weight: bold;
        }
      }
      .progress-line {
        /deep/.el-progress-bar {
          @include wh(370px, 8px);
          border-radius: 5px;
        }
        /deep/.el-progress-bar__inner {
          background: #3d75e5;
          box-shadow: 0px 0px 4px 0px rgba(84, 146, 251, 0.87);
          border-radius: 5px;
        }
      }
    }
  }
</style>
